package com.aoxin.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 自定义拦截器
 *
 * @author Melody
 * @date 2018年3月28日 下午8:51:45
 */
public class SecurityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String url = request.getRequestURI();
        //System.out.println("url:"+url);
        //Cookie cookies[] = request.getCookies();
       if (url.contains("https://open.weixin.qq.com")||url.contains("assets")||url.contains("/web")||url.equals("/slf/index.html") || url.contains("js")||
            url.contains("css")||url.equals("/slf/login.html")||url.contains("img")||url.contains("img") || url.contains("/userIdentifyCodeLogin") || url.contains("/register") || url.contains(".role")
                || url.contains(".menu") || url.contains("/weixinPayNotify") || url.contains("/weiPayTest") || url.contains(".txt")|| url.contains("upload")  || url.contains("uploadBiz")  
                || url.contains("/download")     || url.contains("/error")    || url.contains("insertChildren") 
  
                ) {
    	    if(url.contains(".manage")){
           	String	token = request.getParameter("token");
           	if ((token!=null&& ThreadLocalUtil.getEmployee()==null)) {
       			timeOut(response);
       			return false;
       		}else{
       		 return true;
       		}
           }else{
        	   return true;
           }
        }else if(url.contains("html")){
            if(url.contains("/personal/index.html")){
            	if(CookieUtil.getCookie(CookieUtil.COOKIE_NAME)!=null) {
            		return true;
            	}else {
            		 //商场拦截到首页
                    response.sendRedirect("/slf/index.html");
                    return false;
            	}
               
            }else{
                /*Cookie cookies = CookieUtil.getCookie("slfCookie");
                if(cookies!=null){
                    return true;
                }else{
                    response.sendRedirect("/slf/index.html");
                    return false;
                }*/
                return true;
            }

        }else {
            //所有拦截到首页
            response.sendRedirect("/slf/index.html");
            return false;
        }
    }

    private void timeOut(HttpServletResponse response) throws Exception {
		response.setContentType("text/json; charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		AjaxRes ajaxRes = new AjaxRes();
		ajaxRes.setLogoutMsg(ErrorUtil.LOGIN_EXPIRE);
		response.getWriter().write(ajaxRes.toJSONString());
		response.getWriter().close();
    }

    public static void main(String[] args) {
        String url = "/slf/log";
        //System.out.println(url.substring();
        System.out.println(url.startsWith("/slf"));
    }
}
